<template>
  <div class="app-container">
    <div class="content-wrap my-search">
      <el-form :model="queryParams" size="mini" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="作者" prop="loginId">
          <el-input v-model="queryParams.loginId" placeholder="请输入作者名称" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="发布时间">
          <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8 grid-content">
      <el-col :span="1.5">
        <el-button type="danger" size="mini" plain @click="handleAudit">敏感词管理</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AuditRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属网站Id" align="center" prop="siteId" width="80" show-overflow-tooltip />
      <el-table-column label="文章Id" align="center" prop="articleId" width="150" show-overflow-tooltip />
      <el-table-column label="作者" align="center" prop="loginId" width="200" show-overflow-tooltip />
      <el-table-column label="标题" align="center" prop="header" show-overflow-tooltip />
      <el-table-column label="发布时间" align="center" prop="createdDate" width="155" show-overflow-tooltip />
      <el-table-column label="操作" align="center" width="155" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="getAidutRecordDetail(scope.row)" v-hasPermi="['system:config:edit']">详情</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />


    <!-- 敏感词管理 -->
    <el-drawer title="敏感词管理" :visible.sync="audit" direction="rtl" size="40%" custom-class='drawer-wrap'>
      <div class="">
        <el-row :gutter="10" class="mb8 grid-content content-wrap">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd_audit" v-hasPermi="['system:saveWord:save']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete_audit" v-hasPermi="['system:saveWord:delete']">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="auditData" @selection-change="handleSelectionChange_audit">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" label="序号" width="150" align="center"></el-table-column>
          <el-table-column property="word" label="敏感词" width="150" align="center"></el-table-column>
          <el-table-column property="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete_audit(scope.row)" v-hasPermi="['system:saveWord:delete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total_audit>0" :total="total_audit" :page.sync="queryParams_audit.pageNum" :limit.sync="queryParams_audit.pageSize" @pagination="handleAudit" />
      </div>
    </el-drawer>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="400px" append-to-body :close-on-click-modal='false' :close-on-press-escape='false'>
      <el-form ref="form_audit" :model="form_audit" :rules="rules_audit" label-width="80px" @submit.native.prevent>
        <el-form-item label="敏感词" prop="word">
          <el-input v-model="form_audit.word" placeholder="请输入敏感词" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm_audit">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 文章详情 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open_detail" append-to-body :close-on-click-modal='false' :close-on-press-escape='false' width="700px">
      <div style="height:500px;overflow: auto;">
        <table class="articleDetail_table" align="center" border="1px" cellpadding="10px" cellspacing="0px" width="100%">
          <thead>
            <tr>
              <td align="center">标题</td>
              <td> {{form.header}}</td>
            </tr>
          </thead>
          <tr style="height:500px;overflow: auto;">
            <td width="100" align="center">原始内容</td>
            <td v-html="form.body" style="height:500px;overflow: auto;"></td>
          </tr>
        </table>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import {
  listAudit,
  delAudit,
  addAudit,
  getAuditRecordList,
  getAuditRecord,
} from "@/api/system/audit";

import beautify from "js-beautify";
export default {
  name: "Config",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total_audit: 0,
      // 参数表格数据
      AuditRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false, // 是否显示弹出层
      open_detail: false,
      // 类型数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        configName: undefined,
        configKey: undefined,
        configType: undefined,
      },
      queryParams_audit: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      form_audit: {},
      // 表单校验
      rules: {
        configName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" },
        ],
        configKey: [
          { required: true, message: "参数键名不能为空", trigger: "blur" },
        ],
        configValue: [
          { required: true, message: "参数键值不能为空", trigger: "blur" },
        ],
      },
      rules_audit: {
        word: [{ required: true, message: "敏感词不能为空", trigger: "blur" }],
      },
      //  弹出框
      audit: false,
      //敏感词
      auditData: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getAuditRecordList(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((response) => {
        this.AuditRecordList = response.data.list;
        this.total = response.data.totalCount;
        this.loading = false;
      });
    },
    getAidutRecordDetail(row) {
      getAuditRecord({ id: row.id }).then((res) => {
        this.open_detail = true;
        this.title = "文章详情";
        this.form = res.data;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.form_audit = {};
      this.resetForm("form");
      this.resetForm("form_audit");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    // 多选框选中数据  没什么用
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 多选框选中数据
    handleSelectionChange_audit(selection) {
      this.ids = selection.map((item) => item.id);
      this.names = selection.map((item) => item.word);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const configId = row.configId || this.ids;
      getConfig(configId).then((response) => {
        this.form = response.data;

        // let res = beautify(response.data.configValue, {});
        // this.form.configValue = res;
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.configId != undefined) {
            updateConfig(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids.join(",");
      this.$confirm(
        '是否确认删除参数编号为"' + configIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delConfig(configIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    //敏感词管理
    handleAudit() {
      listAudit(this.queryParams_audit).then((res) => {
        this.auditData = res.data.list;
        this.audit = true;
        this.total_audit = res.data.totalCount;
      });
    },
    handleDelete_audit(row) {
      const ids = row.id || this.ids.join(",");
      const names = row.word || this.names.join(",");
      this.$confirm('是否确认删除敏感词 " ' + names + ' " 的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delAudit({ ids });
        })
        .then(() => {
          this.handleAudit();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd_audit() {
      this.open = true;
      this.title = "新增敏感词";
      this.reset();
    },

    submitForm_audit() {
      this.$refs["form_audit"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateConfig(this.form_audit).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.handleAudit();
            });
          } else {
            addAudit(this.form_audit).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.handleAudit();
            });
          }
        }
      });
    },
  },
};
</script>
 
<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
}
.articleDetail_table {
  display: block;
}
</style>